package br.com.lpoint.web.selectitem;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

public class SelectItemFactory {
	
	
	public static List<SelectItem> createListSelectItems(List<? extends Object> list, String labelAtribute){
		List<SelectItem> itens = new ArrayList<SelectItem>();
		try{
			for (Object object : list) {
				itens.add(new SelectItem(object, invokeLabelMethod(labelAtribute, object)));
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return itens;
	}
	
	
	public static String invokeLabelMethod(String name, Object objeto) throws Exception{
		String primeiraLetra  = 
		name = name.replace(name.substring(0,1), name.substring(0,1).toUpperCase());
		String nomeMetodo = "get"+name;
		Class clazz = objeto.getClass();
		Method metodo = clazz.getMethod(nomeMetodo);
		String result = (String) metodo.invoke(objeto,null);
		return result;
	}

}
